/**
 * 数据库相关类，也可以考虑采用ContentProvider实现。
 */
package com.justspeak.database;

import java.util.ArrayList;
import java.util.List;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.database.sqlite.SQLiteDatabase.CursorFactory;

import com.justspeak.module.CourseDownloadInfo;

public class SqlHelper extends SQLiteOpenHelper {

    private static SqlHelper dbhelper;
    private final static int VERSION = 1;
    private SQLiteDatabase database;

    private SqlHelper(Context context, String name, CursorFactory factory,
	    int version) {
	super(context, name, factory, version);
    }

    public static SqlHelper getInstance(Context context, String tableName) {
	if (dbhelper == null) {
	    dbhelper = new SqlHelper(context, tableName, null, VERSION);
	}
	return dbhelper;
    }

    public void openDB() {
	if (database == null)
	    database = dbhelper.getWritableDatabase();
    }

    @Override
    public void onCreate(SQLiteDatabase db) {
	System.out.println("-------sdfsdfs------------");
	db
		.execSQL("create  table Download_FileInfo(id integer primary key autoincrement,"
			+ "fileid integer,"
			+ "startPosition integer,"
			+ "fileSize integer,"
			+ "fileName text,"
			+ "state integer,"
			+ "bookName text,"
			+ "unitName text)");
    }

    @Override
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
	String deletesql = "drop Download_FileInfo if exist";
	db.execSQL(deletesql);
	onCreate(db);
    }

    /*
     * 读取数据库中没有下载完的文件信息在列表中显示
     */
    public List<CourseDownloadInfo> QueryFileInfoFromDataBase(
	    String wherelimit, String[] parValue) {
	List<CourseDownloadInfo> re_li = new ArrayList<CourseDownloadInfo>();
	this.openDB();
	Cursor cursor = database.query("Download_FileInfo", null, wherelimit,
		parValue, null, null, null);

	while (cursor != null && cursor.moveToNext()) {
	    CourseDownloadInfo info = new CourseDownloadInfo();
	    String fileName = cursor.getString(cursor
		    .getColumnIndex("fileName"));
	    int fileSize = cursor.getInt(cursor.getColumnIndex("fileSize"));
	    int fileState = cursor.getInt(cursor.getColumnIndex("state"));
	    int startPosition = cursor.getInt(cursor
		    .getColumnIndex("startPosition"));
	    info.setFilename(fileName);
	    info.setFilesize(fileSize);
	    info.setStartPosition(startPosition);
	    info.setState(fileState);
	    re_li.add(info);
	}
	cursor.close();
	return re_li;
    }

    /*
     * 更加下载文件的文件名来读取改文件的初始值
     */
    public int queryHadDownloadedByFileName(String filename) {
	openDB();
	Cursor sor = database.query("Download_FileInfo",
		new String[] { "startPosition" }, "fileName=?",
		new String[] { filename }, null, null, null);
	int result = 0;
	while (sor != null && sor.moveToNext()) {
	    result = sor.getInt(0);
	}
	sor.close();
	return result;
    }

    /*
     * 把传过来的文件下载参数存放到数据库
     */
    public void saveNeedDownFileInfo(CourseDownloadInfo info) {
	openDB();
	ContentValues cv = new ContentValues();
	cv.put("fileName", info.getFilename());
	cv.put("fileSize", info.getFilesize());
	cv.put("state", info.getStartPosition());
	cv.put("startPosition", info.getStartPosition());
	cv.put("bookName",info.getBookName());
	cv.put("unitName",info.getUnitName());
	database.insert("Download_FileInfo", null, cv);
    }

    /*
     * 对下载文件的属性更新
     */
    public void updateDownFilePropertyByFileFlag(String[] propertyName,
	    String[] propertyValue, String filename) {
	openDB();
	ContentValues cv = new ContentValues();
	for (int i = 0; i < propertyName.length; i++) {
	    cv.put(propertyName[i], propertyValue[i]);
	}

	database.update("Download_FileInfo", cv, "fileName=?",
		new String[] { filename });
    }
}
